using System;
using System.Collections;
using System.Collections.Generic;
using Main.Dialogue;
using Main.Event;
using UnityEngine;
using UnityEngine.Playables;

namespace Main.Timeline
{
    [Serializable]
    public class DialogueBehavior : PlayableBehaviour
    {

        private PlayableDirector playableDirector;
        public DialoguePiece dialoguePiece;

        public override void OnPlayableCreate(Playable playable)
        {
            playableDirector = playable.GetGraph().GetResolver() as PlayableDirector;
        }

        public override void OnBehaviourPlay(Playable playable, FrameData info)
        {
            if (Application.isPlaying)
            {
                MainEventSystem.CallShowDialogueEvent(dialoguePiece);
                if (dialoguePiece.hasToPause)
                {
                    TimelineManager.Instance.PauseTimeLine(playableDirector);
                }
                else
                {
                    MainEventSystem.CallShowDialogueEvent(null);
                }
            }
        }

        public override void ProcessFrame(Playable playable, FrameData info, object playerData)
        {
            if (Application.isPlaying)
            {
                TimelineManager.Instance.IsDone = dialoguePiece.isDone;
            }
        }

        public override void OnBehaviourPause(Playable playable, FrameData info)
        {
            if (Application.isPlaying)
                MainEventSystem.CallShowDialogueEvent(null);
        }

        public override void OnGraphStart(Playable playable)
        {
            if (Application.isPlaying)
                MainEventSystem.CallUpdateGameStateEvent(GameState.Pause);
        }

        public override void OnGraphStop(Playable playable)
        {
            if (Application.isPlaying)
                MainEventSystem.CallUpdateGameStateEvent(GameState.GamePlay);
        }
    }
}